package com.matrix.lbs.controller;

import com.matrix.api.lbs.dto.ShopInfoUpdateDTO;
import com.matrix.api.lbs.vo.NearbyShopWithInfoVO;
import com.matrix.common.domain.PageResult;
import com.matrix.common.domain.Result;
import com.matrix.lbs.service.LbsService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 有点甜
 * @since 2025/7/25
 */
@RestController
@RequiredArgsConstructor
public class LbsController {

    private final LbsService lbsService;


    @PutMapping("/internal/lbs/shop/upsert")
    public Result<Void> upsertShopDocument(@RequestBody ShopInfoUpdateDTO dto) {
        lbsService.upsertShopDocument(dto);
        return Result.success();
    }

    @GetMapping("/app/lbs/shops/nearby")
    public Result<List<NearbyShopWithInfoVO>> findNearbyShops(
            @RequestParam("latitude") double latitude,
            @RequestParam("longitude") double longitude,
            @RequestParam("distance") double distance,
            @RequestParam(name = "sortType", defaultValue = "default") String sortType) {
        return Result.success(lbsService.findNearbyShops(latitude, longitude, distance, sortType));
    }

    @GetMapping("/app/lbs/shops/search")
    public Result<PageResult<NearbyShopWithInfoVO>> searchShops(
            @RequestParam("keyword") String keyword,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) Double latitude,
            @RequestParam(required = false) Double longitude,
            @RequestParam(name = "sortType", defaultValue = "default") String sortType) {
        return Result.success(lbsService.searchShops(keyword, pageNum, pageSize, latitude, longitude, sortType));
    }

    @GetMapping("/app/lbs/shops/recommendations")
    public Result<PageResult<NearbyShopWithInfoVO>> getRecommendedShops(
            @RequestHeader("X-User-ID") Long userId,
            @RequestParam("latitude") double latitude,
            @RequestParam("longitude") double longitude,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return Result.success(lbsService.findRecommendedShops(userId, latitude, longitude, pageNum, pageSize));
    }

    @GetMapping("/app/lbs/shops/suggestions")
    public Result<List<String>> getSearchSuggestions(@RequestParam("prefix") String prefix) {
        return Result.success(lbsService.getSearchSuggestions(prefix));
    }
}
